package com.example.happyprogress;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class happyprogress extends AppCompatActivity {

    ProgressBar progressBar;
    TextView progressText;
    ImageButton startBtn;
    ImageButton switchBtn;

    private int mProgressStatus = 0;
    private Handler mHandler;
    private ExecutorService ThreadPool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_happyprogress);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressText = (TextView)findViewById(R.id.progress_text);
        startBtn = (ImageButton) findViewById(R.id.startBtn);
        switchBtn = (ImageButton) findViewById(R.id.switchBtn);

        mHandler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==getResources().getInteger(R.integer.progress_status_running))
                {
                    progressBar.setProgress(mProgressStatus);
                    progressText.setText(getResources().getString(R.string.pg_title_running));
                }
                if (msg.what==getResources().getInteger(R.integer.progress_status_over))
                {
                    progressText.setText(getResources().getString(R.string.pg_title_over));
                    progressBar.setVisibility(View.GONE);
                    startBtn.setVisibility(View.VISIBLE);
                    switchBtn.setVisibility(View.VISIBLE);

                }
            }
        };

        ThreadPool = Executors.newSingleThreadExecutor();
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    mProgressStatus = doWork();
                    Message m = new Message();
                    if (mProgressStatus<getResources().getInteger(R.integer.progress_max)){
                        m.what = getResources().getInteger(R.integer.progress_status_running);
                        mHandler.sendMessage(m);
                    }
                    else
                    {
                        m.what = getResources().getInteger(R.integer.progress_status_over);
                        mHandler.sendMessage(m);
                        break;
                    }
                }
            }
            private int doWork(){
                mProgressStatus +=Math.random()*10;
                try {
                    Thread.sleep(200);
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
                return mProgressStatus;
            }
        });




    }
}